package com.atguigu.flink;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo04 {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(3);

        DataStreamSource<Integer> ds = env.fromElements(1, 2, 3, 4,5,6,7,8,9);
        ds.map(new RichMapFunction<Integer, Integer>() {

            //RichMapFunction相当于mappartition，只不过mappartition中是一个分区对数据库建立一次连接
            //而这里是一个任务的并行度就会对数据创建一个连接
            @Override
            public void open(Configuration parameters) throws Exception {
                System.out.println("开始创建数据库连接！！！");
            }

            @Override
            public void close() throws Exception {
                System.out.println("开始关闭数据库连接！！！");
            }

            //这里是数据的处理方法，每来一条数据就进行处理！
            @Override
            public Integer map(Integer value) throws Exception {
                return value;
            }
        }).print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
